package exteditapp.state;

import exteditapp.AppletIterator;
import exteditapp.FileDefault;
import exteditapp.action.ActionError;

/**
 * State - file idle or not changed (instantiated then last edit was N seconds ago)
 * @author Arturas Smorgun, arturas@smorgun.lt
 */
public class StateError extends IState {

	IState nextState;
	
	int maxLifetime;
	
	/**
	 * Default constructor
	 * @param iterator is system iterator, which performs all actions
	 * @param file this is file, which state we will hold
	 */
	public StateError(AppletIterator iterator, FileDefault file) {
		super(iterator, file);
		nextState = new StateIdling(this.iterator, this.file, false);
		maxLifetime = 0;
		start();
	}
	public StateError(AppletIterator iterator, FileDefault file, boolean doRegister){
		super(iterator, file, doRegister);
		start();
	}
	
	protected void start(){
		this.setAction(new ActionError());
		this.actionMenu.setEnabled(false);
	}
	
	public void setup(String label){
		this.actionMenu.setText(label);
	}
	
	public void setup(String label, IState nextState){
		this.setup(label);
		if(nextState != null)
			this.nextState = nextState;
	}
	
	public void setup(String label, IState nextState, int maxLifetime){
		this.setup(label, nextState);
		if(maxLifetime >= 0)
			this.maxLifetime = maxLifetime;
	}
	
	/**
	 * Handling of new state
	 */
	public boolean handle() {
		if(this.getLifetime() > 0 && this.getLifetime() % maxLifetime == 0){
			this.nextState.resetLifetime();
			this.file.setCurrentState(nextState);
		}
		return false;
	}
	
}
